package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;
import java.util.Calendar;

@Repository("DetectstandardDAO")
@Transactional
public class DetectstandardDAOImpl extends AbstractJpaDao<Detectstandard> implements DetectstandardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Detectstandard.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public DetectstandardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Detectstandard findDetectstandardById(Integer id) throws DataAccessException {

		return findDetectstandardById(id, -1, -1);
	}

	@Transactional
	public Detectstandard findDetectstandardById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectstandardById", startResult, maxRows, id);
			return (Detectstandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByName(String name) throws DataAccessException {

		return findDetectstandardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByName", startResult, maxRows, name);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByNameContaining(String name) throws DataAccessException {

		return findDetectstandardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByDate(Calendar date) throws DataAccessException {

		return findDetectstandardByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByDate(Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByDate", startResult, maxRows, date);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByMin(BigDecimal min) throws DataAccessException {

		return findDetectstandardByMin(min, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByMin(BigDecimal min, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByMin", startResult, maxRows, min);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByMax(BigDecimal max) throws DataAccessException {

		return findDetectstandardByMax(max, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByMax(BigDecimal max, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByMax", startResult, maxRows, max);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByDescription(String description) throws DataAccessException {

		return findDetectstandardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardByDescriptionContaining(String description)
			throws DataAccessException {

		return findDetectstandardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findDetectstandardByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@Transactional
	public Detectstandard findDetectstandardByPrimaryKey(Integer id) throws DataAccessException {
		return findDetectstandardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Detectstandard findDetectstandardByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectstandardByPrimaryKey", startResult, maxRows, id);
			return (Detectstandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectstandardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectstandard> findAllDetectstandards() throws DataAccessException {

		return findAllDetectstandards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findAllDetectstandards(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectstandards", startResult, maxRows);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectstandard> findDetectstandardsByFeature(Integer featureId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectstandardsByFeature", start, max, featureId);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectstandard> findDetectstandardsByFood(Integer foodId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectstandardsByFood", start, max, foodId);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectstandard> findDetectstandardsByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectstandardsByUnit", start, max, unitId);
		return new LinkedHashSet<Detectstandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectstandard> findDetectstandardByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Detectstandard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Detectstandard> findDetectstandardsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Detectstandard.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Detectstandard> detectstandards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return detectstandards;
	}

	@Transactional
	public Detectstandard findDetectstandardByPhysicalPrimaryKey(Integer foodId, Integer featureId, Calendar date)
			throws DataAccessException {
		return findDetectstandardByPhysicalPrimaryKey(foodId, featureId, date, -1, -1);
	}

	@Transactional
	public Detectstandard findDetectstandardByPhysicalPrimaryKey(Integer foodId, Integer featureId, Calendar date,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectstandardByPhyscialPrimaryKey", startResult, maxRows, foodId,
					featureId, date);
			return (Detectstandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Detectstandard entity) {
		return true;
	}
}
